package com.tanhua.server.controller;

import com.tanhua.manage.domain.vo.MomentVo;
import com.tanhua.manage.domain.vo.PageResult;
import com.tanhua.manage.domain.vo.PublishVo;
import com.tanhua.manage.domain.vo.VisitorVo;
import com.tanhua.server.service.CommentService;
import com.tanhua.server.service.MomentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/movements")
public class MomentController {

    @Autowired
    private MomentService momentService;
    @Autowired
    private CommentService commentService;
    /**
     * 发布动态
     * @return
     */
    @PostMapping
    public ResponseEntity postMoment(PublishVo publishVo, MultipartFile[] imageContent){
        momentService.postMoment(publishVo,imageContent);
        return ResponseEntity.ok(null);
    }

    /**
     * 好友 动态
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResponseEntity queryFriendPublishList(@RequestParam(value = "page") Long page,
                                                 @RequestParam(value = "pagesize" )Long pageSize){
        PageResult<MomentVo> pageResult = momentService.queryFriendPublishList(page,pageSize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询推荐动态
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity queryRecommendPublishList(@RequestParam(value = "page")Long page,
                                                    @RequestParam(value = "pagesize") Long pageSize){
        PageResult<MomentVo> pageResult = momentService.queryRecommendPublishList(page,pageSize);
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 查询我的动态
     */
    @GetMapping("/all")
    public ResponseEntity queryMyPublishList(@RequestParam(value = "page")Long page,
                                             @RequestParam(value = "pagesize") Long pageSize,
                                             Long userId){
        PageResult<MomentVo> pageResult = momentService.queryMyPublishList(userId,page,pageSize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 点赞功能
     */
    @GetMapping("/{publishId}/like")
    public ResponseEntity like(@PathVariable String publishId){
        long likeCount = commentService.like(publishId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消点赞功能
     */
    @GetMapping("/{publishId}/dislike")
    public ResponseEntity dislike(@PathVariable String publishId){
        long likeCount = commentService.dislike(publishId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 喜欢功能
     */
    @GetMapping("/{publishId}/love")
    public ResponseEntity love(@PathVariable String publishId){
        long loveCount = commentService.love(publishId);
        return ResponseEntity.ok(loveCount);
    }

    /**
     * 取消喜欢功能
     */
    @GetMapping("/{publishId}/unlove")
    public ResponseEntity unlove(@PathVariable String publishId){
        long loveCount = commentService.unlove(publishId);
        return ResponseEntity.ok(loveCount);
    }

    /**
     * 单条动态查询
     */
    @GetMapping("/{publishId}")
    public ResponseEntity findById(@PathVariable String publishId){
        // APK会自动发送/movements/visitors， 如果谁看过我的功能没有完成的话，就会被我们这个方式捕获, 所以要先过滤visitors
        // 防止后台报错
        if ("visitors".equals(publishId)){
            return ResponseEntity.ok(null);
        }
        MomentVo vo = momentService.findById(publishId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 谁看过我
     */
    @GetMapping("/visitors")
    public ResponseEntity visitors(){
        List<VisitorVo> visitorList = momentService.queryVisitors();
        return ResponseEntity.ok(visitorList);
    }

}
